#!/bin/bash

# This script checks quine programs.
# https://en.wikipedia.org/wiki/Quine_(computing)
#
# Usage:
#
#   check-quine <input file> <command>
#   check-quine foo.dx dex -- script --allow-errors
#
# Technically, quines are programs that take no input and produce their own
# source as output. This script instead runs a command with an input file and
# checks whether the output has no diff with the input file.
#
# If the output of the command applied to the input file has no diff with the
# input file, then the input file is a quine and the script exits with a zero
# success status.
#
# Otherwise, if the input file is not a quine, the script prints the diff to
# stdout and exits with a non-zero error status.

tmpout=$(mktemp)
errout=$(mktemp)

if ${@:2} $1 > $tmpout 2> $errout ; then
    # We check for differences up to timing outputs from the %time or %bench
    # commands, because those are expected to vary from run to run.
    misc/check-no-diff \
      <(grep -vE "> (Compile|Run) time: " $1) \
      <(grep -vE "> (Compile|Run) time: " $tmpout)
    status=$?
else
    status=$?
    cat $tmpout
fi

cat $errout

rm $errout
rm $tmpout

exit $status
